﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Rotempco.ProjectCreator.Logic
{
    public class CreateNewProjectLogic
    {
        public static void CreateProject(string rotempcoProjectRoot, string newProjectRoot, string projectName)
        {
            newProjectRoot += @"\" + projectName;

            CopyDirectory(rotempcoProjectRoot + @"\Rotempco.Core\Rotempco.Core", newProjectRoot + @"\" + projectName + ".Core", projectName);
            CopyDirectory(rotempcoProjectRoot + @"\Rotempco.Templates\Rotempco.BaseClass", newProjectRoot + @"\" + projectName + ".BaseClass", projectName);
            CopyDirectory(rotempcoProjectRoot + @"\Rotempco.Templates\Rotempco.TestBench", newProjectRoot + @"\" + projectName + ".TestBench", projectName);
            CopyDirectory(rotempcoProjectRoot + @"\Rotempco.Templates\Rotempco.Website", newProjectRoot + @"\" + projectName + ".Website", projectName);
            File.Copy(rotempcoProjectRoot + @"\Rotempco.Templates\Rotempco.sln", newProjectRoot + @"\" + projectName + ".sln");
            File.Copy(rotempcoProjectRoot + @"\Rotempco.Templates\Rotempco.mapping", newProjectRoot + @"\" + projectName + ".mapping");

            CorrectCoreReference(newProjectRoot + @"\" + projectName + ".sln");
            RecursiveRenameFromHell(newProjectRoot, "Rotempco", projectName);
        }

        private static void CorrectCoreReference(string coreFile)
        {
            string fileContent = File.ReadAllText(coreFile);
            File.WriteAllText(coreFile, fileContent.Replace(@"..\Rotempco.Core\Rotempco.Core\Rotempco.Core.csproj", @"Rotempco.Core\Rotempco.Core.csproj"),Encoding.Default);
        }

        private static void RecursiveRenameFromHell(string root, string originalString, string replacementString)
        {
            string[] files = Directory.GetFiles(root);

            foreach (string file in files)
            {
                FileInfo fileInfo = new FileInfo(file);
                if (fileInfo.Extension.Equals(".cs", StringComparison.InvariantCultureIgnoreCase) ||
                    fileInfo.Extension.Equals(".csproj", StringComparison.InvariantCultureIgnoreCase) ||
                    fileInfo.Extension.Equals(".sln", StringComparison.InvariantCultureIgnoreCase) ||
                    fileInfo.Extension.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase) ||
                    fileInfo.Extension.Equals(".master", StringComparison.InvariantCultureIgnoreCase) ||
                    fileInfo.Extension.Equals(".config", StringComparison.InvariantCultureIgnoreCase) ||
                    fileInfo.Extension.Equals(".asax", StringComparison.InvariantCultureIgnoreCase) ||
                    fileInfo.Extension.Equals(".ascx", StringComparison.InvariantCultureIgnoreCase) ||
                    fileInfo.Extension.Equals(".mapping", StringComparison.InvariantCultureIgnoreCase))
                {
                    string fileContent = File.ReadAllText(file);
                    if(fileInfo.Extension.Equals(".mapping", StringComparison.InvariantCultureIgnoreCase))
                    {
                        fileContent = fileContent.Replace("[MainProjectFile]", @"\" + replacementString + @".BaseClass\" + replacementString + @".BaseClass.csproj");
                    }

                    File.WriteAllText(file, fileContent.Replace(originalString, replacementString), Encoding.Default);
                }                
            }

            foreach (string directory in Directory.GetDirectories(root))
            {
                RecursiveRenameFromHell(directory, originalString, replacementString);
            }
        }

        private static void CopyDirectory(string fromDirectory, string toDirectory, string projectName)
        {
            if (!Directory.Exists(toDirectory))
                Directory.CreateDirectory(toDirectory);

            string[] files = Directory.GetFiles(fromDirectory);

            foreach (string file in files)
            {
                FileInfo fileInfo = new FileInfo(file);
                if (fileInfo.Extension.Equals(".suo", StringComparison.InvariantCultureIgnoreCase) ||
                    fileInfo.Extension.Equals(".user", StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                if (fileInfo.Extension.Equals(".csproj", StringComparison.InvariantCultureIgnoreCase))
                {
                    File.Copy(fileInfo.FullName, toDirectory + @"\" + fileInfo.Name.Replace("Rotempco", projectName));
                }
                else
                {
                    File.Copy(fileInfo.FullName, toDirectory + @"\" + fileInfo.Name);
                }
            }

            foreach (string directory in Directory.GetDirectories(fromDirectory))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(directory);

                if (directoryInfo.Name.Equals("bin", StringComparison.InvariantCultureIgnoreCase) ||
                     directoryInfo.Name.Equals("obj", StringComparison.InvariantCultureIgnoreCase) ||
                        directoryInfo.Name.Equals(".svn", StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                CopyDirectory(directory, toDirectory + @"\" + directoryInfo.Name, projectName);
            }
        }

    }
}
